<?php

namespace app\common\model\mysql;

use app\common\business\DataAuth;
use think\Model;

class SysConfig extends Model
{
    /**
     * 自动生成写入时间
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 新增前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeInsert($model){
        $userData = (new DataAuth())->getUserDetails();
        $model->create_by = $userData['userName'];
    }

    /**
     * 更新前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeUpdate($model)
    {
        $userData = (new DataAuth())->getUserDetails();
        $model->update_by = $userData['userName'];
    }

    //根据字典类型查询字典数据信息
    public function getConfigList($configKey){
        if(empty($configKey)){
            return false;
        }
        $where = [
            "config_key" =>$configKey,
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    /**
     * 查询参数数据列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getNormalConfigList($where, $field,$limit,$page){
        $order = [
            "config_id" =>"asc",
        ];
        $result = $this->where($where)->field($field)->order($order)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);
        return $result;
    }

    /**
     * 根据$$config_name去数据库查询是否存在这条记录
     * @param $dict_type
     * @param $dict_label
     * @return SysDictData|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getConfigByNameModel($config_name,$config_id){
        if(empty($config_name)){
            return false;
        }
        $where = [
            "config_name" =>$config_name,
        ];
        if($config_id){
            $result = $this->where("config_id", "<>", $config_id)->where($where)->find();
        }else{
            $result = $this->where($where)->find();
        }
        return $result;
    }

    /**
     * 根据$config_key去数据库查询是否存在这条记录
     * @param $config_key
     * @return SysDictData|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    //
    public function getConfigByKeyModel($config_key,$config_id){
        if(empty($config_key)){
            return false;
        }
        $where = [
            "config_key" =>$config_key,
        ];
        if($config_id){
            $result = $this->where("config_id", "<>", $config_id)->where($where)->find();
        }else{
            $result = $this->where($where)->find();
        }
        return $result;
    }

    /**
     * 根据id获取参数数据信息
     * @param $id
     * @return SysDictData|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getConfigIdData($id){
        if(empty($id)){
            return false;
        }
        $where = [
            "config_id" =>$id,
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    /**
     * 删除参数数据信息
     * @param $ids
     * @return bool
     */
    public function DeleteConfigIds($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['config_id', 'in', $ids]
        ];
        $result = $this->where($where)->delete();
        return $result;
    }
}
